﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.ComponentModel;


namespace BadHabit.WPFTools {

    
    /// <summary>
    /// Extends the usual ComboBox to suport a different Template for the Selected Item.
    /// </summary>
    public class ComboBox : System.Windows.Controls.ComboBox {

        public static readonly DependencyProperty MaxLengthProperty = DependencyProperty.Register("EditableTextBox", typeof(int), typeof(BadHabit.WPFTools.ComboBox), new UIPropertyMetadata(int.MaxValue));

        /// <summary>
        /// DependencyProperty for the select item template.
        /// </summary>
        public static DependencyProperty SelectedItemTemplateProperty = DependencyProperty.Register("SelectedItemTemplate", typeof(DataTemplate), typeof(BadHabit.WPFTools.ComboBox));

        /// <summary>
        /// DependencyProperty for the dropdown background.
        /// </summary>
        public static DependencyProperty DropDownBackgroundProperty = DependencyProperty.Register("DropDownBackground", typeof(Brush), typeof(BadHabit.WPFTools.ComboBox));


        [Description("Maximum length of the text in the EditableTextBox.")]
        [Category("Editable ComboBox")]
        [DefaultValue(int.MaxValue)]
        public int MaxLength {

            [System.Diagnostics.DebuggerStepThrough]
            get { return (int)this.GetValue(MaxLengthProperty); }

            [System.Diagnostics.DebuggerStepThrough]
            set {
                this.SetValue(MaxLengthProperty, value);
                this.ApplyMaxLength();
            }
        }

        protected TextBox EditableTextBox { get { return this.GetTemplateChild("PART_EditableTextBox") as TextBox; } }

        public override void OnApplyTemplate() { base.OnApplyTemplate(); this.ApplyMaxLength(); }

        private void ApplyMaxLength() { if (this.EditableTextBox != null) { this.EditableTextBox.MaxLength = this.MaxLength; } }

        /// <summary>
        /// Get and set the datatemplate for the selected item.
        /// This is a dependency property.
        /// </summary>
        public DataTemplate SelectedItemTemplate {
            get {
                return (DataTemplate) GetValue(SelectedItemTemplateProperty);
            }
            set {
                SetValue(SelectedItemTemplateProperty, value);
            }
        }

        public Brush DropDownBackground {
            get {
                return (Brush)GetValue(DropDownBackgroundProperty);
            }
            set {
                SetValue(DropDownBackgroundProperty, value);
            }
        }


        /// <summary>
        /// Updates the data source.
        /// </summary>
        private void UpdateDataSource() {
            BindingExpression expression = GetBindingExpression(ComboBox.TextProperty);
            if (expression != null) {
                expression.UpdateSource();
            }
        }

        /// <summary>
        /// Updates the Text property binding when the selection changes.
        /// </summary>
        /// <param name="e">A SelectionChanged EventArgs.</param>
        protected override void OnSelectionChanged(SelectionChangedEventArgs e) {
            base.OnSelectionChanged(e);
            this.UpdateDataSource();
        }

        protected override void OnKeyUp(System.Windows.Input.KeyEventArgs e) {
            base.OnKeyUp(e);
            if (e.Key == System.Windows.Input.Key.Return || e.Key == System.Windows.Input.Key.Enter) {
                this.UpdateDataSource();
            }
        }
        
    }
}
